import os,sys,glob,shutil


d = {
    'apache-ant':'ANT_HOME',
    'apache-maven':'M2_HOME',
    'apache-tomcat':'CATALINA_HOME',
    'gradle':'GRADLE_HOME',    
    'griffon':'GRIFFON_HOME',
    'groovy':'GROOVY_HOME',
    'grails':'GRAILS_HOME',
}

exclude_bin = ['CATALINA_HOME',]

szdir_base = 'c:/j'

l_szenv = []
l_szpath = []

def normpath(s):
    return s.replace('/','\\')

for szmatch, szenv in d.iteritems():
    szfind = '%(szdir_base)s/%(szmatch)s*'%locals()
    l = glob.glob(szfind)
    l = filter(os.path.isdir,l)
    if len(l)!=1:
        print '[WARN]multi matched',l
    else:
        szhome = l[0]
        szhome = normpath(szhome)
        print szenv,szhome
        l_szenv.append('%(szenv)s=%(szhome)s'%locals())
        szbin = '%(szhome)s/bin'%locals()
        szbin = normpath(szbin)
        print szbin,os.path.isdir(szbin)
        l_szpath.append('%%%(szenv)s%%\\bin'%locals())
        
print '-'*80
print '\n'.join(l_szenv)
print '-'*80
print ';'.join(l_szpath)
